package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import util.GetTime;

public class FreeBoardDAO {
	
	public boolean insert(FreeBoard fb)  {
		
		Connection conn = ConnectionManager.getConnection();
	//@@ String sql1 = "insert into freeboard(cono, fbname, fbrefno, mid, fbtitle,fbdate, fbcontent, fbcounter) values(?,?,?,?,?,now(),?,0)";
		String sql1 = "insert into freeboard(fbno, cono, fbname, fbrefno, mid, fbtitle,fbdate, fbcontent, fbcounter) values(fbseq.NEXTVAL,?,?,?,?,?,?,?,0)";
		String sql2 = "update freeboard set fbrefno=fbno where fbrefno=0";
		boolean result = false;
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(sql1);
			PreparedStatement pstmt2 = conn.prepareStatement(sql2);
			int cono = 5;
			pstmt.setInt(1, fb.getCono());
			//pstmt.setInt(1, cono);//##임시로 cono번호를 5로..
			pstmt.setString(2,fb.getFbname());
			pstmt.setInt(3, fb.getFbrefno());
			pstmt.setString(4, fb.getMid());
			pstmt.setString(5, fb.getFbtitle());
			pstmt.setTimestamp(6, GetTime.nowTime);//##
	   //@@ pstmt.setString(6, fb.getFbcontent());
			pstmt.setString(7, fb.getFbcontent());
			
			pstmt.executeUpdate();
			pstmt2.executeUpdate();				
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}
	
	public boolean updateReadCount(Long fbno) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			stmt.executeUpdate("update freeboard set fbcounter=fbcounter+1 where fbno=" + fbno);
			///////////////////////////////////////////////
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
	}

	public boolean updateFreeBoard(FreeBoard fb) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(
					"update freeboard set fbtitle=?, fbcontent=? " +
					"where fbno=?");
			pstmt.setString(1, fb.getFbtitle());
			pstmt.setString(2, fb.getFbcontent());
			pstmt.setInt(3, fb.getFbno());
			
			pstmt.executeUpdate();			
			///////////////////////////////////////////////			
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return true;
	}
	
	
	public int delete(int cono){
		int result = 0;
		String sql1 = "delete from freecomment where fbno in (select fbno from freeboard where cono=?)";
		String sql2 = "delete from freeboard where cono=?";	
		
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement pstmt1 = conn.prepareStatement(sql1);
			PreparedStatement pstmt2 = conn.prepareStatement(sql2);
			
			pstmt1.setInt(1, cono);
			
			pstmt2.setInt(1, cono);
			
			pstmt1.executeUpdate();
			pstmt2.executeUpdate();
			
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
		return result;
	}
	
	
	public boolean delete(int cono , String mid) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			stmt.executeUpdate("delete from freeboard where cono=" + cono + " and mid='"+mid+"'");
			///////////////////////////////////////////////			
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return true;
	}
	
	
	
	public boolean delete(Long fbno) {
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			stmt.executeUpdate("delete from freeboard where fbno=" + fbno);
			///////////////////////////////////////////////			
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return true;
	}
	
	public boolean deleteReplies(Long fbno) {
		Connection conn = ConnectionManager.getConnection();
		String sql1 = "delete from freecomment where fbno in (select fbno from freeboard where fbrefno="+fbno+")";		
		String sql2 = "delete from freeboard where fbrefno=" + fbno+" and fbno<>"+fbno;
		
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt1 = conn.prepareStatement(sql1);
			PreparedStatement pstmt2 = conn.prepareStatement(sql2);
			pstmt1.executeUpdate();
			pstmt2.executeUpdate();
			///////////////////////////////////////////////
			
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return true;
	}
	
	public FreeBoard  getFreeBoard(Long fbno){
		FreeBoard freeBoard = null;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from freeboard where fbno=" + fbno);
			if(rs.next()) {
				freeBoard = new FreeBoard();
				freeBoard.setFbno(rs.getInt(1));
				freeBoard.setCono(rs.getInt(2));
				freeBoard.setFbname(rs.getString(3));
				freeBoard.setFbrefno(rs.getInt(4));
				freeBoard.setMid(rs.getString(5));
				freeBoard.setFbtitle(rs.getString(6));
				freeBoard.setFbdate(rs.getString(7).substring(0, 19));
				freeBoard.setFbcontent(rs.getString(8));
				freeBoard.setFbcounter(rs.getInt(9));	
			}
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return freeBoard;
	}
	
	
	public int getPreview(FreeBoard fb){
		int previewNumber = 0;
		
		String sql ="select max(fbno) from (select fbno from freeboard where cono=? and fbname=? and fbno=fbrefno) f where fbno<?";
		Connection conn = null;
		try{
			conn = ConnectionManager.getConnection();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, fb.getCono());
			pstmt.setString(2, fb.getFbname());
			pstmt.setInt(3, fb.getFbno());
			ResultSet rs = pstmt.executeQuery();
			
			int no = 0;
			while(rs.next()){
				no = rs.getInt(1);				
			}			 
			previewNumber = no!=0?no:fb.getFbno();
			
			return previewNumber;
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {				 
				e.printStackTrace();
			}
		}
		return fb.getFbno();
	}

	public int getNext(FreeBoard fb){
		int nextNumber = 0;
		String sql = "select min(fbno) from (select fbno from freeboard where cono=? and fbname=? and fbno=fbrefno) f where fbno>?";
		
		Connection conn = null;
		try{
			conn = ConnectionManager.getConnection();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, fb.getCono());
			pstmt.setString(2, fb.getFbname());
			pstmt.setInt(3, fb.getFbno());
			ResultSet rs = pstmt.executeQuery();
			
			int no = 0;
			while(rs.next()){
				no = rs.getInt(1);				
			}			 
			nextNumber = no!=0?no:fb.getFbno();
			
			return nextNumber;
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {				 
				e.printStackTrace();
			}
		}
		return fb.getFbno();
	}
	
	/**
	 * 조성일
	 * 자유게시판 전체 카운트 취득
	 * */
	public long getTotalRows() {
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select count(*) from freeboard");
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}	
	
	
	public int getFbNo(int cono , String mid) {
		int result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery("select fbno from freeboard Where cono="+cono+" and mid='"+mid+"'");
			
			rs.next();
			result = rs.getInt(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}
	
	
	/**
	 * 조성일
	 * 자유게시판 전체 카운트 취득
	 * */
	public long getTotalRows(int cono) {
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT count(*) FROM freeboard where cono="+cono);
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}	
	
	//##추가함 getRownum(int cono, String fbname, long fbno)에서 사용하기 위해
	public long getTotalRows(int cono, String fbname) {
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select count(*) from freeboard" +
					" where cono="+cono+" and fbname='"+fbname+"'");
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}	
	
	
	public long getTotalRows(FreeBoard fb) {
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			///////////////////////////////////////////////
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select count(*) from freeboard" +
					" where cono="+fb.getCono()+" and fbname='"+fb.getFbname()+"'");
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}	
	
	/**
	 * 페이지 리스트 추출 메소드
	 * */
	public Vector<FreeBoard> getBoardListPage(FreeBoard fb, long currentPage, long rowsPerPage){
		 
		Vector<FreeBoard> vector = new Vector<FreeBoard>();
		Connection conn = ConnectionManager.getConnection();
		
		//#*
		int rownum = (int)getTotalRows(fb) + 1;//글번호_마지막번호~1
		int index = 0;	     //모든 행에 1번부터 끝행까지 번호를 임시로 준다
		long startindex = 0; //보여질 페이지의 첫행의 인덱스
		long endindex = 0;   //보여질 페이지의 마지막행의 인덱스
		startindex = (currentPage - 1) * rowsPerPage + 1;
		endindex = currentPage * rowsPerPage;
		//*#
		
		/*@@String sql = "select f.* from(select free.*,(@rownum:=@rownum+1) rownum " +
				"from (select freeboard.*,(@rownum:=0) rnum  from freeboard where cono=? and fbname=?) free) " +
				"f order by fbrefno desc limit "+rowsPerPage*(currentPage-1)+","+rowsPerPage;*/
		/*원래주석처리된 
		String sql = "select f.* from( select free.*,(@rownum:=@rownum+1) rownum " +
				"from ( select freeboard.*, (@rownum:=0) rnum " +
				"from freeboard where cono=? and fbname=? and fbrefno=0) free) " +
				"f order by rownum desc limit "+rowsPerPage*(currentPage-1)+","+rowsPerPage;
		*/
		//#*
		String sql = "select * from freeboard where cono=? and fbname=? order by fbrefno desc,fbno asc";//*#
		
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setLong(1, fb.getCono());
			pstmt.setString(2, fb.getFbname());
			
			ResultSet rs = pstmt.executeQuery();
			FreeCommentDAO fcDAO = new FreeCommentDAO();
			while(rs.next()) {
				//#*
				index += 1;		//## 모든 행에 인덱스번호 부여 1~끝까지
				rownum -= 1;	
				if(index >= startindex && index <= endindex){	//##
					System.out.println("rownum = " + rownum); //*#
					FreeBoard freeBoard = new FreeBoard();
					freeBoard.setFbno(rs.getInt(1));
					freeBoard.setCono(rs.getInt(2));
					freeBoard.setFbname(rs.getString(3));
					freeBoard.setFbrefno(rs.getInt(4));
					freeBoard.setMid(rs.getString(5));
					freeBoard.setFbtitle(rs.getString(6));
					freeBoard.setFbdate(rs.getString(7).substring(0,19));
					freeBoard.setFbcontent(rs.getString(8));
					freeBoard.setFbcounter(rs.getInt(9));
					freeBoard.setRownum(rownum);
					freeBoard.setFbcomments(fcDAO.getTotalRows(freeBoard.getFbno()));
					vector.add(freeBoard);
				}
			}
			rs.close();
			pstmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}
	
	/**
	 * 검색시 페이지 리스트 추출 메소드
	 * */
	//@@ public Vector<FreeBoard> getBoardListPageByMid(FreeBoard fb, long currentPage, long rowsPerPage,String word){
	public Vector<FreeBoard> getBoardListPage(FreeBoard fb ,long currentPage, long rowsPerPage,String word,String search,long totalrows){
		System.out.println("search = "+search);
		//*#
		int rownum = (int)totalrows + 1;
		int index = 0;	     //모든 행에 1번부터 끝행까지 번호를 임시로 준다
		long startindex = 0; //보여질 페이지의 첫행의 인덱스
		long endindex = 0;   //보여질 페이지의 마지막행의 인덱스
		startindex = (currentPage - 1) * rowsPerPage + 1;
		endindex = currentPage * rowsPerPage;
		//#*
		Vector<FreeBoard> vector = new Vector<FreeBoard>();
		Connection conn = ConnectionManager.getConnection();
		/*String sql = "select f.* from(select free.*,(@rownum:=@rownum+1) rownum " +
				"from (select freeboard.*,(@rownum:=0) rnum  from freeboard where cono=? and fbname=?) free) " +
				"f where mid=? order by fbrefno desc limit "+rowsPerPage*(currentPage-1)+","+rowsPerPage;*/
		String sql = "select * from freeboard where cono=? and fbname=? and "+search+" like '%"+word+"%' order by fbrefno desc,fbno asc";
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setLong(1, fb.getCono());
			pstmt.setString(2, fb.getFbname());
			
			
			ResultSet rs = pstmt.executeQuery();
			FreeCommentDAO fcDAO = new FreeCommentDAO();
			while(rs.next()) {
				//#*
				index += 1;		//## 모든 행에 인덱스번호 부여 1~끝까지
				rownum -= 1;	
				if(index >= startindex && index <= endindex){	//##
					System.out.println("@rownum = " + rownum); //*#
					FreeBoard freeBoard = new FreeBoard();
					freeBoard.setFbno(rs.getInt(1));
					freeBoard.setCono(rs.getInt(2));
					freeBoard.setFbname(rs.getString(3));
					freeBoard.setFbrefno(rs.getInt(4));
					freeBoard.setMid(rs.getString(5));
					freeBoard.setFbtitle(rs.getString(6));
					freeBoard.setFbdate(rs.getString(7).substring(0,19));
					freeBoard.setFbcontent(rs.getString(8));
					freeBoard.setFbcounter(rs.getInt(9));
					//@@ freeBoard.setRownum(rs.getInt(11));
					freeBoard.setRownum(rownum);
					freeBoard.setFbcomments(fcDAO.getTotalRows(freeBoard.getFbno()));
					vector.add(freeBoard);
				}
			}
			rs.close();
			pstmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}
	
	/**
	 * 페이지 리스트 추출 메소드
	 * */
	//## 필요없음
	/*@@ public Vector<FreeBoard> getBoardListPageByFbtitle(FreeBoard fb, long currentPage, long rowsPerPage,String word){
		
		Vector<FreeBoard> vector = new Vector<FreeBoard>();
		Connection conn = ConnectionManager.getConnection();
		String sql = "select f.* from(select free.*,(@rownum:=@rownum+1) rownum " +
				"from (select freeboard.*,(@rownum:=0) rnum  from freeboard where cono=? and fbname=?) free) " +
				"f where fbtitle like '%"+word+"%' order by fbrefno desc limit "+rowsPerPage*(currentPage-1)+","+rowsPerPage;
		
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setLong(1, fb.getCono());
			pstmt.setString(2, fb.getFbname());			
			
			ResultSet rs = pstmt.executeQuery();
			FreeCommentDAO fcDAO = new FreeCommentDAO();
			while(rs.next()) {
				FreeBoard freeBoard = new FreeBoard();
				freeBoard.setFbno(rs.getInt(1));
				freeBoard.setCono(rs.getInt(2));
				freeBoard.setFbname(rs.getString(3));
				freeBoard.setFbrefno(rs.getInt(4));
				freeBoard.setMid(rs.getString(5));
				freeBoard.setFbtitle(rs.getString(6));
				freeBoard.setFbdate(rs.getString(7).substring(0,19));
				freeBoard.setFbcontent(rs.getString(8));
				freeBoard.setFbcounter(rs.getInt(9));
				freeBoard.setRownum(rs.getInt(11));
				freeBoard.setFbcomments(fcDAO.getTotalRows(freeBoard.getFbno()));
				vector.add(freeBoard);
			}
			rs.close();
			pstmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}*/
	
	public static long getTotalRowsByMid(String mid, long cono, String fbname){
		String sql = "select count(*) from freeboard where cono=? and fbname=? and mid=?"; 
		Connection conn = ConnectionManager.getConnection();
		long result = 0;
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setLong(1, cono);
			pstmt.setString(2, fbname);
			pstmt.setString(3, mid);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				result = rs.getLong(1);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
		return result;
	}

	public static long getTotalRowsByTitle(String fbtitle, long cono, String fbname){
		String sql = "select count(*) from freeboard where cono=? and fbname=? and fbtitle like '%"+fbtitle+"%'"; 
		Connection conn = ConnectionManager.getConnection();
		long result = 0;
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setLong(1, cono);
			pstmt.setString(2, fbname);
			
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				result = rs.getLong(1);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public Vector<FreeBoard> getTopFiveListPage(FreeBoard fb, long currentPage, long rowsPerPage){
		
		Vector<FreeBoard> vector = new Vector<FreeBoard>();
		Connection conn = ConnectionManager.getConnection();
		
		//#*
		int rownum = (int)getTotalRows(fb) + 1;//글번호_마지막번호~1
		int count = 0;
		//*#
		
		
		/*String sql = "select f.* from(select free.*,(@rownum:=@rownum+1) rownum " +
				"from (select freeboard.*,(@rownum:=0) rnum  from freeboard where cono=? and fbname=?) free) " +
				"f order by fbdate desc limit "+rowsPerPage*(currentPage-1)+","+rowsPerPage;*/
		/*
		String sql = "select f.* from( select free.*,(@rownum:=@rownum+1) rownum " +
				"from ( select freeboard.*, (@rownum:=0) rnum " +
				"from freeboard where cono=? and fbname=? and fbrefno=0) free) " +
				"f order by rownum desc limit "+rowsPerPage*(currentPage-1)+","+rowsPerPage;
		*/
		String sql = "select * from freeboard " +
				"where cono=? and fbname=? order by fbrefno desc,fbno asc";
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setLong(1, fb.getCono());
			pstmt.setString(2, fb.getFbname());
			
			ResultSet rs = pstmt.executeQuery();
			FreeCommentDAO fcDAO = new FreeCommentDAO();
			while(rs.next()) {
				
				//#*
				rownum -= 1;
				count++;
				if(count >= 1 && count <= 5){
					System.out.println("rownum = " + rownum); //*#
				
					FreeBoard freeBoard = new FreeBoard();
					freeBoard.setFbno(rs.getInt(1));
					freeBoard.setCono(rs.getInt(2));
					freeBoard.setFbname(rs.getString(3));
					freeBoard.setFbrefno(rs.getInt(4));
					freeBoard.setMid(rs.getString(5));
					freeBoard.setFbtitle(rs.getString(6));
					freeBoard.setFbdate(rs.getString(7).substring(0,19));
					freeBoard.setFbcontent(rs.getString(8));
					freeBoard.setFbcounter(rs.getInt(9));
					freeBoard.setRownum(rownum);
					//@@freeBoard.setRownum(rs.getInt(11));
					freeBoard.setFbcomments(fcDAO.getTotalRows(freeBoard.getFbno()));
					vector.add(freeBoard);
				}
			}
			rs.close();
			pstmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return vector;
	}
	
	public String[] getBoardMenus(int cono){
		Vector<String> v = new Vector<String>(); 
		String[] menus = null;
		String sql = "select distinct fbname from freeboard where cono=?";
		Connection conn = null;
		try{
			conn = ConnectionManager.getConnection();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, cono);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				v.add(rs.getString(1));
			}
			menus = new String[v.size()];
			for(int i=0;i<menus.length;i++){
				menus[i] = v.get(i);
			}
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return menus;
	}
	
	public int setBoardName(int cono, String fbname, String newname){
		int result = 0;
		String sql = "update freeboard set fbname=? where cono=? and fbname=?";
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, newname);
			pstmt.setInt(2, cono);
			pstmt.setString(3, fbname);
			
			result = pstmt.executeUpdate();
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public int deleteBoard(int cono, String fbname){
		int result = 0;
		String sql1 = "delete from freecomment where fbno in (select fbno from freeboard where cono=? and fbname=?)";
		String sql2 = "delete from freeboard where cono=? and fbname=?";
	
		
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement pstmt1 = conn.prepareStatement(sql1);
			PreparedStatement pstmt2 = conn.prepareStatement(sql2);
			
			pstmt1.setInt(1, cono);
			pstmt1.setString(2, fbname);
			
			pstmt2.setInt(1, cono);
			pstmt2.setString(2, fbname);
			
			pstmt1.executeUpdate();
			pstmt2.executeUpdate();
			
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/*public String getRownum(int cono, String fbname, long fbno){
		Connection conn = null;
		String result = null;
		String sql = "select f.* from(select free.*,(@rownum:=@rownum+1) rownum " +
		"from (select freeboard.*,(@rownum:=0) rnum  from freeboard where cono=? and fbname=?) free) f " +
		"where fbno=? order by fbrefno desc";
		try {
			conn = ConnectionManager.getConnection();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, cono);
			pstmt.setString(2, fbname);
			pstmt.setLong(3, fbno);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				result = rs.getString(11);
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
		return result;
	}*/
	//위메소드 재구현 자유게시판의 리스트에서 글 하나를 클릭시 상세보기할때 리스트상의 rownum 을 가져오기 위해
	public String getRownum(int cono, String fbname, long fbno){ 
		
		int rownum = (int)getTotalRows(cono,fbname) + 1;
		String result = "";
		Connection conn = ConnectionManager.getConnection();
		String sql = "select fbno from freeboard where cono=? and fbname=? order by fbrefno desc, fbno asc";
		
		try {
			///////////////////////////////////////////////
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setLong(1, cono);
			pstmt.setString(2, fbname);
			ResultSet rs = pstmt.executeQuery();			
			
			while(rs.next()) {
				rownum -= 1;
				System.out.println("fbRownum : "  + rownum);
				if(rs.getInt("fbno")== fbno){
					System.out.println("해당 fbRownum : "  + rownum);
					result = Integer.toString(rownum);
				}
			}
			rs.close();
			pstmt.close();
			///////////////////////////////////////////////
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.close(); } catch(Exception e) {}
		}
		return result;
	}
	
	
	public static void main(String[] args){
		FreeBoardDAO fbDAO = new FreeBoardDAO();
		System.out.println(fbDAO.getBoardMenus(2).length);
	}
	
}
